package logic

import (
	"context"
	"fmt"
	"profile/model/mysql"

	"profile/userrpc/internal/svc"
	"profile/userrpc/userrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUPLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUPLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUPLogic {
	return &UserUPLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserUPLogic) UserUP(in *userrpc.UserUPRequest) (*userrpc.UserUPResponse, error) {
	// todo: add your logic here and delete this line

	var finduser mysql.User
	mysql.DB.Where("username = ?", in.Username).Find(&finduser)
	if finduser.ID == 0 {
		return nil, fmt.Errorf("该账号不存在")
	}
	if finduser.Password != in.Password {
		return nil, fmt.Errorf("密码错误")
	}

	return &userrpc.UserUPResponse{
		Id: int64(finduser.ID),
	}, nil
}
